package cn.com.wangluotongxin.mall.service.impl;

import cn.com.wangluotongxin.mall.model.MallGoodsInfo;
import cn.com.wangluotongxin.mall.mapper.MallGoodsInfoMapper;
import cn.com.wangluotongxin.mall.model.MallGoodsStandard;
import cn.com.wangluotongxin.mall.model.MallQuickQuery;
import cn.com.wangluotongxin.mall.rabbitmq.pojo.GoodsInfoPojo;
import cn.com.wangluotongxin.mall.service.IMallGoodsInfoService;
import cn.com.wangluotongxin.mall.service.IMallGoodsMxService;
import cn.com.wangluotongxin.mall.service.IMallGoodsStandardService;
import cn.com.wangluotongxin.mall.service.IMallQuickQueryService;
import cn.com.wangluotongxin.mall.vo.GoodsInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 网络通信-商城APP-商品信息 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2024-11-05
 */
@Service
public class MallGoodsInfoServiceImpl extends ServiceImpl<MallGoodsInfoMapper, MallGoodsInfo> implements IMallGoodsInfoService {

    @Autowired
    private IMallQuickQueryService quickQueryService;
    @Autowired
    private IMallGoodsStandardService goodsStandardService;
    @Autowired
    private IMallGoodsMxService goodsMxService;

    @Override
    public GoodsInfoVo detail(Integer id) {
        return baseMapper.detail(id);
    }

    @Override
    public MallGoodsInfo getByAdminGoodsId(Integer tenantId, Integer adminGoodsId) {
        return this.getOne(new LambdaQueryWrapper<MallGoodsInfo>()
                .eq(MallGoodsInfo::getTenantId, tenantId)
                .eq(MallGoodsInfo::getAdminGoodsId, adminGoodsId)
                .last("limit 0,1"));
    }

    @Override
    public void goodsInfoSync(GoodsInfoPojo pojo) {
        MallGoodsInfo goodsInfo = this.buildGoodsInfo(pojo);
        if(goodsInfo.getId() != null && goodsInfo.getId() > 0) {
            this.updateById(goodsInfo);
        } else {
            this.save(goodsInfo);
        }
        // 同步规格信息
        MallGoodsStandard goodsStandard = goodsStandardService.goodsStandardSync(goodsInfo.getId(), pojo);

        // 生成商品明细
        goodsMxService.goodsMxSync(goodsInfo, goodsStandard);
    }

    private MallGoodsInfo buildGoodsInfo(GoodsInfoPojo pojo) {
        MallGoodsInfo newGoodsInfo = new MallGoodsInfo();

        MallGoodsInfo goodsInfo = this.getByAdminGoodsId(pojo.getTenantId(), pojo.getAdminGoodsId());
        if(goodsInfo != null){
            newGoodsInfo.setId(goodsInfo.getId());
        }

        newGoodsInfo.setTenantId(pojo.getTenantId());
        newGoodsInfo.setTenantName(pojo.getTenantName());
        newGoodsInfo.setAdminGoodsId(pojo.getAdminGoodsId());
        newGoodsInfo.setGoodsId(pojo.getGoodsId());
        newGoodsInfo.setGoodsNo(pojo.getGoodsNo());
        newGoodsInfo.setGoodsName(pojo.getGoodsName());

        MallQuickQuery quickQuery = quickQueryService.getBySource(pojo.getAdminQuickQueryId());
        if(quickQuery == null) {
            if(goodsInfo != null && goodsInfo.getQuickQueryId() != null) {
                this.update(new LambdaUpdateWrapper<MallGoodsInfo>()
                        .set(MallGoodsInfo::getQuickQueryId, null)
                        .set(MallGoodsInfo::getQuickQueryName, null)
                        .eq(MallGoodsInfo::getId, goodsInfo.getId()));
            }
        } else {
            newGoodsInfo.setQuickQueryId(quickQuery.getId());
            newGoodsInfo.setQuickQueryName(quickQuery.getQuickName());
        }

        newGoodsInfo.setCatalogId(pojo.getCatalogId());
        newGoodsInfo.setCatalogPath(pojo.getCatalogPath());
        newGoodsInfo.setCatalogName(pojo.getCatalogName());
        newGoodsInfo.setGoodsImages(pojo.getGoodsImages());
        newGoodsInfo.setGoodsRemark(pojo.getGoodsRemark());
        return newGoodsInfo;
    }
}
